<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// autor/i etapy: Matúš Novák
// 29.11.2013

class User extends CI_Controller {
   
  //presmeruje na login
	public function index(){
		 $this->user_view();
	}
  
  //ak je lognuty, presmeruje na usera inac restricted
  public function user_view(){
  	if ($this->session->userdata('is_logged_in')){
        $this->template->make_template("", 'user_main');
    } else
        redirect('main/restricted');
  }

  //vypise view s kolektivami, tu je hlavny view, ktory sa dalej cleni na zmen,pridaj,vymaz
  public function kolektiv(){
    if ($this->session->userdata('is_logged_in')){
        $this->load->model('model_user');
        $data['kk'] = $this->model_user->get_kolektivy();     
        $this->template->make_template($data, 'user_kolektiv');
    } else{
        redirect('main/restricted');
    }
  }
  
  //nacita view na pridanie kolektivu ak je lognuty, inac restricted
  public function addkolektiv(){
      if ($this->session->userdata('is_logged_in')){
          $this->template->make_template("", 'user_pridajKolektiv');
      } else{
          redirect('main/restricted');
      }
  }

  //zobrazi view rozvrhUzivatel ak je login
  public function mojrozvrh(){
    if ($this->session->userdata('is_logged_in')){
        $this->template->make_template("", 'user_rozvrh');
    } else{
        redirect('main/restricted');
    }
  }
  
  //zobrazi view na prihlasenie
  public function prihlasenie_kolektiv(){
    if ($this->session->userdata('is_logged_in')){
        $this->template->make_template("", 'user_prihlasenieNaUdalost');
    } else{
        redirect('main/view_restricted');
    }  
  }
  
  public function kolektiv_prihlasenie(){
  
  
  
        //TODO
  }
  
  //nastavi pravidla pre pridanie kolektivu a skontroluje, ak sa podari, prida do DB kolektiv s userovym ID
  public function kolektiv_validation(){
      $this->load->library('form_validation');
      $this->load->model('model_user');
      
      $this->form_validation->set_rules('nazov','Nazov','required|trim');
      $this->form_validation->set_rules('pocet',"Pocet",'integer|required|trim');  
      $this->form_validation->set_rules('skolaKolektiv',"SkolaKolektiv",'required|trim');
      $this->form_validation->set_rules('vek',"Vek",'integer|required|trim');
      $this->form_validation->set_rules('zameranie',"Zameranie",'required|trim');
      if ($this->input->post('pocet') <= 0){
          $this->template->make_template("", 'user_pridajKolektiv');
      }
      
      if ($this->form_validation->run()){
          $temp = $this->session->userdata('id');
          if ($this->model_user->add_kolektiv($temp)){
              //$this->load->view('members');
              echo "kolektiv pridany";
              redirect('user/kolektiv');
          }
      } else {
          $this->template->make_template("", 'user_pridajKolektiv');
      }
  }
  
  //ak sa zatlaci button v kolektiv viewe, rozozna, ktory buttom a presmeruje na zmen alebo vymaz
  public function kolektiv_pressed(){
    if ($this->session->userdata('is_logged_in')){
      $this->load->library('form_validation');
      $this->form_validation->set_rules('kolektivy','Kolektivy','required|trim');
      if ($this->form_validation->run()){
          //rozoznavanie buttonov, ak je NULL nezatlacil sa
          if ($this->input->post("kolektiv_change") != NULL){
              redirect('user/kolektiv_change/'.$this->input->post('kolektivy'));
          }
          else if ($this->input->post("kolektiv_delete") != NULL){
              redirect('user/kolektiv_delete/'.$this->input->post('kolektivy'));
          } else{
              echo "no button";
          } 
      } else redirect('user/kolektiv');
    } else {
        redirect('main/view_restricted');
    }
  }
  
  //zmena kolektivu, nacita kolektivy usera a zmeni ten, ktory si uzivatel vybral
  public function kolektiv_change($input){
      if ($this->session->userdata('is_logged_in')){
          $select = $input;
          $this->load->model('model_user');
          
          $data['koc'] = $this->model_user->get_kolektivy();
          $temp = array();
          foreach($data['koc'] as $prvok){
              $temp[] =  $prvok;
          }  
          $kolektiv = array();
          $kolektiv = $this->model_user->get_info_kolektiv($temp[$select]); // jeden kolektiv
          $idk = $this->model_user->get_id_kolektivu($temp[$select]); //id kolektivu
          $this->session->set_userdata('uid',$idk);
          $this->template->make_template($kolektiv, 'user_zmenKolektiv');
      } else {
          redirect('main/view_restricted');
      }
  }
  
  //vymaze zvoleny kolektiv, podobny princip ako zmena
  public function kolektiv_delete($input){
      if ($this->session->userdata('is_logged_in')){
          $select = $input;
          $this->load->model('model_user');
          $data['kod'] = $this->model_user->get_kolektivy();
          $temp = array();
          foreach($data['kod'] as $prvok){
              $temp[] =  $prvok;
          }
          //print_r($temp);
          
          $kolektiv = $this->model_user->get_id_kolektivu($temp[$select]);
          $udalosti = $this->model_user->get_id_rozvrh_udalosti($kolektiv);
          print_r($udalosti);
          
          $pocet_kolektiv = $this->model_user->get_pocet_kolektiv($kolektiv);
          
          foreach ($udalosti as $row){
             $zaplnenie = $this->model_user->get_zaplnenie($row);
             $lud =  $zaplnenie - $pocet_kolektiv;
             $this->model_user->update_zaplnenie($row,$lud);
          }
          if ($this->model_user->deleteKolektiv($kolektiv)){
              redirect('user/kolektiv');
          } else redirect('user/kolektiv');
          
      }   else {
          redirect('main/view_restricted');
      }
  }
  
  //skontroluje ci su udaje pre zmenu validne,mozne, ak ano, zmeni kolektiv
  public function kolektiv_zmena_validation(){
  if ($this->session->userdata('is_logged_in')){
      $this->load->library('form_validation');
      $this->load->model('model_user');
      
      $this->form_validation->set_rules('nazov','Nazov','required|trim');
      $this->form_validation->set_rules('pocet',"Pocet",'integer|required|trim');  
      $this->form_validation->set_rules('skolaKolektiv',"SkolaKolektiv",'required|trim');
      $this->form_validation->set_rules('vek',"Vek",'integer|required|trim');
      $this->form_validation->set_rules('zameranie',"Zameranie",'required|trim');
      
      if ($this->input->post('pocet') <= 0){
          $this->template->make_template("", 'user_zmenKolektiv');
      }
      if ($this->form_validation->run()){
          $temp = $this->session->userdata('uid');
          
          //odtialto  
          $max_ludi = $this->model_user->get_maxLudi($temp);
          
          $udalosti = $this->model_user->get_id_rozvrh_udalosti($temp);
          print_r($udalosti);
          $pocet_kolektiv = $this->model_user->get_pocet_kolektiv($temp);
          $pocet_kolektiv_change = $this->input->post('pocet');
          
          foreach ($udalosti as $row){
            $zaplnenie = $this->model_user->get_zaplnenie($row);
            $lud =  $zaplnenie - $pocet_kolektiv + $pocet_kolektiv_change;
            if ($lud <= $max_ludi) {
            }
            else {redirect('user/overkill');}
          }   
          //sem
  
          if ($this->model_user->change_kolektiv($temp)){
              $this->session->unset_userdata('uid');
  
          //odtialto 
              foreach ($udalosti as $row){
                $zaplnenie = $this->model_user->get_zaplnenie($row);
                $lud =  $zaplnenie - $pocet_kolektiv + $pocet_kolektiv_change;
                $this->model_user->update_zaplnenie($row,$lud);
              }
          //sem
          
              echo "kolektiv zmeneny";
              redirect('user/kolektiv'); 
          } 
      } else {
          //redirect('user/kolektiv');
          $this->template->make_template("", 'user_zmenKolektiv');
      }  
      
      }   else {
          redirect('main/view_restricted');
      } 
  } 
  
  public function overkill(){
     if ($this->session->userdata('is_logged_in')){
         $this->template->make_template("", 'user_overkill');
    } else {
        redirect('main/view_restricted');
    }
  } 
  
  public function prekryte(){
     if ($this->session->userdata('is_logged_in')){
         $this->template->make_template("", 'user_prekryte');
    } else {
        redirect('main/view_restricted');
    }
  }
  //prihlasi sa na udalost
  public function udalost_prihlasenie($id_ud, $cas, $id_casu_ud){
       if ($this->session->userdata('is_logged_in')){
          $this->load->library('form_validation');
          $this->form_validation->set_rules('kolektiv','Kolektiv','required|trim');
         if ($this->form_validation->run()){ 
          $this->load->model('model_user');
          $this->load->model('model_rozvrh');
          $max_ludi = $this->model_user->get_maxLudi($id_ud);
          $zaplnenie = $this->model_user->get_zaplnenie($id_casu_ud);
          $trvanie = $this->model_user->get_trvanie($id_casu_ud);
          
          $data['kod'] = $this->model_user->get_kolektivy();
          $temp = array();
          foreach($data['kod'] as $prvok){
              $temp[] =  $prvok;
          }
          //print_r($temp);
          $select = $this->input->post('kolektiv');
          $kolektiv = $this->model_user->get_id_kolektivu($temp[$select]);
          $pocet_kolektiv = $this->model_user->get_pocet_kolektiv($kolektiv);
          
          echo $kolektiv;
          echo $id_ud;
          echo $cas;
          echo "<br>";
          echo $max_ludi;
          echo "<br>";
          echo $zaplnenie;
          echo "<br>";
          echo $pocet_kolektiv;
          
          if ($zaplnenie + $pocet_kolektiv <= $max_ludi) {
            // pridaj, zapln, prihlas, fixni, nastav cas
             $min = $this->model_rozvrh->StrToMins($cas);
             $novy_cas = $min + $trvanie;
             $ludia = $zaplnenie+$pocet_kolektiv;
             if ($cas == 'NULL'){
                $cas = $this->model_user->get_cas_od($id_casu_ud);
             }
             
             $data = array(
                'id' => $id_casu_ud,
                'cas_od' => $cas,
                'cas_do' => $this->model_rozvrh->MinsToStr($novy_cas),
                'zaplnenie' => $ludia
            );
             if ($this->model_user->is_not_in($kolektiv,$id_casu_ud)){
                if (!$this->model_user->prekryva_sa($kolektiv, $id_casu_ud)){
                   $this->model_user->update_udalost($data);
                   $this->model_user->prihlas_udalost($id_casu_ud,$kolektiv);
                }
                else{
                  redirect('user/prekryte');
                }
             }
             redirect('rozvrh');
          }
          else {redirect('user/overkill');}
      } else redirect('user/kolektiv_error');
    } else {
        redirect('main/view_restricted');
    } 
    }
  
  public function kolektiv_error(){
       $this->template->make_template("", 'user_kolektiv_kill');
  }
    
  public function udalost_odhlasenie($id_casu_ud){
    if ($this->session->userdata('is_logged_in')){
        $this->load->model('model_user');
        $kolektivy = $this->model_user->get_id_kolektivy();
        print_r($kolektivy);
        foreach ($kolektivy as $row){
          $pocet_kolektiv = $this->model_user->get_pocet_kolektiv($row);
          $zaplnenie = $this->model_user->get_zaplnenie($id_casu_ud);
          $lud =  $zaplnenie - $pocet_kolektiv;
          if ($this->model_user->is_not_in($row,$id_casu_ud)){
          }else $this->model_user->update_zaplnenie($id_casu_ud,$lud);
          $this->model_user->logout_all($row,$id_casu_ud);
        }
        redirect('rozvrh'); 
    } else {
        redirect('main/view_restricted');
    } 
  }

  //test TODO vymazat
  public function konflikt_test() {
	$this->load->model('model_user');
	$str = "<b>Test:</b><br />";
	$str .= ($this->model_user->prekryva_sa(31, 73)?'t':'f');
	$str .= ' == true<br />';
	$str .= ($this->model_user->prekryva_sa(31, 80)?'t':'f');
	$str .= ' == true<br />';
	$str .= ($this->model_user->prekryva_sa(31, 69)?'t':'f');
	$str .= ' == false<br />';
	$str .= ($this->model_user->prekryva_sa(31, 70)?'t':'f');
	$str .= ' == false<br />';
	$data['dump'] = $str;	
	 $this->template->make_template($data, 'user_rozvrh'); 
  }
  
  
  //test^
}
